var app = getApp();
const $ = require('../../utils/utils');
const config = require('../../config');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: '',
        inviteCount: 0,
        bonus: 0,
        showInfo: false
    },

   
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

   
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getInviteInfo()
        this.gotoAuthor()
    },

 
    /***
    * methods
    * **/
    gotoAuthor: function () {
        let that = this
        wx.getSetting({
            success: (res) => {
                console.log('getSetting', res);
                if (res.authSetting['scope.writePhotosAlbum'] == false) {
                    wx.showModal({
                        title: '提示',
                        content: '需要您设置用户授权才能保存图片哦~',
                        showCancel: false,
                        confirmText: '去设置',
                        success: function (opt) {
                            wx.openSetting({
                                success: (res) => {

                                }
                            });
                        }
                    });
                }
            },
        });

    },

    getInviteInfo: function () {
        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken')
        }
        let url = $.getUrl(config.getInviteInfo, data)
        $.get(url).then(data => {
            console.log('inviteInfo', data)
            this.setData({
                imgUrl: data.detail.inviteUrl,
                inviteCount: data.detail.inviteCount,
                bonus: data.detail.bonus
            })

        })
    },

    saveImg: function (e) {
        let inviteImg = this.data.imgUrl
        wx.showModal({
            title: '提示',
            content: '是否要保存图片',
            success: function (res) {
                if (res.confirm) {
                    wx.getImageInfo({
                        src: inviteImg,
                        success: function (ret) {
                            var path = ret.path;
                            wx.saveImageToPhotosAlbum({
                                filePath: path,
                                success(result) {
                                    console.log('saveImg', result)
                                }, fail(err) {
                                    console.log('saveErr', err)
                                }
                            })
                        }
                    })
                } else if (res.cancel) {

                }
            }
        })

    },

    inviteInfo: function () {
        this.setData({
            showInfo: true
        })
    },
    cancelInviteInfo: function () {
        this.setData({
            showInfo: false
        })
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})